package com.agy.filemanager.controller;

import com.agy.filemanager.common.TreeUtil;
import com.agy.filemanager.entity.DcBaseRoleMenu;
import com.agy.filemanager.entity.TreeModel;
import com.agy.filemanager.service.DcBaseRoleMenuService;
import com.agy.filemanager.toolClass.Result;
import com.agy.filemanager.toolClass.ResultUtil;
import com.github.pagehelper.Page;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Zhang Fan
 * @date 2018\8\8 0008 16:18
 * @desc 角色-菜单controller层
 */
@Controller
@RequestMapping("/systemRoleMenu")
public class DcBaseRoleMenuController {

    @Autowired
    DcBaseRoleMenuService dcBaseRoleMenuService;
    /**
     * 分页-获取角色信息
     * @param request
     * @return
     */
    @PostMapping("/getRoleInfo")
    @ResponseBody
    public Map<String,Object> getRoleInfo(HttpServletRequest request){
        int pageSize =Integer.parseInt(request.getParameter("pageSize"));
        int pageNum =Integer.parseInt(request.getParameter("pageNum"));
        Page list = this.dcBaseRoleMenuService.getRoleInfo(pageNum,pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("total",list.getTotal());
        map.put("rows",list.getResult());
        return map;
    }

    /**
     * 获取菜单树
     * @return
     */
    @PostMapping("/menuTreeInfo")
    @ResponseBody
    public Result agyOrganiseInfo(){
        List<TreeModel> list = TreeUtil.getTree(this.dcBaseRoleMenuService.menuTreeInfo());
        return ResultUtil.success(list, "成功");
    }

    /**
     * 通过角色ID获取DcBaseRoleMenu实体
     * @return
     */
    @PostMapping("/getInfoByRoleId")
    @ResponseBody
    public Result getInfoByRoleId(HttpServletRequest request){
        String id = request.getParameter("id");
        List<DcBaseRoleMenu> list = dcBaseRoleMenuService.getInfoByRoleId(id);
        return ResultUtil.success(list, "成功");
    }

    /**
     * 保存：角色-菜单
     * @return
     */
    @PostMapping("/addRoleMenuInfo")
    @ResponseBody
    public Result addRoleMenuInfo(HttpServletRequest request){
        String roleId = request.getParameter("roleId");
        String arry = request.getParameter("menuIdList");
        arry = arry.substring(2,arry.length()-2);
        String[] menuIdList = arry.split("\",\"");
        try{
            this.dcBaseRoleMenuService.addRoleMenuInfo(menuIdList,roleId);
            return ResultUtil.success(null, "设置成功");
        }catch(Exception ex){
            return ResultUtil.error("新增失败");
        }
    }
}
